package com.cj.webapp.user.service;

import com.cj.webapp.user.controller.UserServlet;
import com.cj.webapp.user.dao.UserDao;
import com.cj.webapp.user.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * Created by dell on 2018/1/12.
 */
public class UserServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    private UserDao userDao=new UserDao();

    /**
     * 验证用户名密码
     * @param dto
     * @return
     */
    public String queryUser(UserDto dto){

        UserDto userDto=new UserDto();
        try {
            if(dto!=null){
                userDto=userDao.findUserByName(dto);
                logger.info("findUserByName查询出来的用户名："+userDto.getName()+"密码："+userDto.getPassword());
                if(userDto!=null){
                    if(userDto.getPassword().equals(dto.getPassword().trim())){
                      return "0";
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return "1";
    }

    /**
     * 查询用户信息
     * @param dto
     * @return
     */
    public UserDto getUserMessage(UserDto dto){

        UserDto userDto=new UserDto();
        try {
            if(dto!=null){
                userDto=userDao.findUserByName(dto);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return userDto;
    }

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    public int addUser(UserDto userDto){

         try{
             if(userDto!=null){
                 List<UserDto> dto = userDao.findUserByPhone(userDto);
                 if(dto.size()==0){
                     userDao.addUser(userDto);
                 }else {
                     return 0;
                 }
             }
         }catch (Exception e ){
             e.printStackTrace();
         }

        return 1;
    }
}
